﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class MenuScreen : GameScreen
    {
#region Properties
        private List<Texture2D> _ListBGMenu;
        public List<Texture2D> ListBGMenu
        {
            get { return _ListBGMenu; }
            set { _ListBGMenu = value; }
        }

        private SpriteFont _menuFont;
        public SpriteFont MenuFont
        {
            get { return _menuFont; }
            set { _menuFont = value; }
        }

        private MenuComponent _menuComponent;
        private double _preGameTime;
        private int _indexBG;
#endregion

#region  Initilation
        public MenuScreen(Game game, MainGame mainGame, SpriteBatch spriteBatch, List<Texture2D> listBGMenu, SpriteFont menuFont)
            : base(game, mainGame, spriteBatch)
        {
            _game = game;
            _mainGame = mainGame;
            _spriteBatch = spriteBatch;
            _ListBGMenu = listBGMenu;
            _menuFont = menuFont;
            _indexBG = 0;
            _preGameTime = 0;

            //Init Menucomponent
            _menuComponent = new MenuComponent(_game, menuFont, _spriteBatch);
            Components.Add(_menuComponent);
            //Event su kien click cua button item menu click
            for (int i = 0; i < _menuComponent.ListItemMenu.Count; i++)
            {
                _menuComponent.ListItemMenu[i].Selected += new Button.IsSelected(ItemMenu_Selected);
            }
            //Sound background
            MediaPlayer.Play(ManagerGame.managerSound._menuStart);
        }       
#endregion

#region Methods
        void ItemMenu_Selected(int input)
        {
            if (input == 0) // start game
            {
                MediaPlayer.Stop();
                _mainGame.ShowScreen(3);
                return;
            }
            _mainGame.ShowScreen(input);
        }
#endregion

#region Update & Draw
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            double time = gameTime.TotalGameTime.TotalSeconds;
            if (time - _preGameTime > 1)
            {
                _preGameTime = time;
                _indexBG ++;
                if (_indexBG >= 3) _indexBG = 0;
            }
            _spriteBatch.Draw(_ListBGMenu[_indexBG], Vector2.Zero, Color.White);
            base.Draw(gameTime);
            _spriteBatch.End();
        }
#endregion
    }
}
